import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import * as _ from 'lodash';
import * as moment from 'moment';
import {
  DailyReportServiceProxy,
  SearchInputReport
} from '../../../shared/service-proxies/service-proxies';

@Component({
  selector: 'app-month-water-report',
  templateUrl: './month-water-report.component.html',
  styleUrls: ['./month-water-report.component.scss']
})
export class MonthWaterReportComponent implements OnInit {

  monthDataParam: SearchInputReport = new SearchInputReport();//获取月报数据参数
  nowDate: any = new Date();
  aaa: any;
  averageList: any;
  averageMonthList: any = [];
  pingjun: any = [];
  maxDataList: any = [];
  dayWaterList: any = [];//当月平均日供水量及日最大供水量信息表（万m³）;

  constructor(
    private router: Router,
    private _DailyReportServiceProxy: DailyReportServiceProxy,
  ) { }

  ngOnInit() {
    this.getMeanData();
  }

  goBack() {
    this.router.navigateByUrl('home')
  }

  //当月平均日供水量及日最大供水量信息表
  getMeanData() {
    this.monthDataParam.start = this.nowDate;
    this.monthDataParam.year = true;
    // this.monthDataParam.tagfields = ["供水量"];
    this._DailyReportServiceProxy.searchWaterReportSum(this.monthDataParam)
      .subscribe((res) => {
        console.log("当月平均日供水量及日最大供水量信息表", res);
        let grouped = _.groupBy(res, m => m.month);
        let average = _.toArray(grouped).map(n => {
          this.aaa = _.max(n);
          // console.log("aaa", this.aaa)
        });
        // console.log("average", average);
        let result = _.uniq(res.map(m => m.month)).map(d => {
          let sum = _.sumBy(grouped[d], s => parseInt(s.value));
          let year = res.filter(m => m).map(m => m.year)[0];
          let max = _.maxBy(grouped[d], s => parseInt(s.value)).value

          return {
            month: d,
            year: year,
            name: "月供水量",
            value: (sum / 31).toFixed(2),
            average1: max
          };
        });
        this.averageList = result;
        // console.log("month", grouped);
        console.log("result", result);
        for (let i in this.averageList) {
          this.averageMonthList.push(this.averageList[i].month + "月");

          this.pingjun.push(this.averageList[i].value);

          this.maxDataList.push(this.averageList[i].average1)
        }
        this.pingjun.unshift("平均日供水量");
        this.maxDataList.unshift("最大日供水量");
        // this.dayWaterList.push(this.averageMonthList)
        this.dayWaterList.push(this.pingjun);
        this.dayWaterList.push(this.maxDataList);
        console.log("this.dayWaterList", this.dayWaterList)
      })
  }

}
